package com.example.demo.service.impl;

import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.Announcement;
import com.example.demo.pojo.PageBean;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import com.example.demo.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByphoneNumber(String phonenumber) {
        User u = userMapper.findByphoneNumber(phonenumber);
        return u;

    }

    @Override
    public User findByemail(String email) {
        User u = userMapper.findByemail(email);
        return u;
    }

    @Override
    public void register(String realname, String password, String address,String phonenumber) {
        String avatar = "https://a.520gexing.com/uploads/allimg/2019080515/he0mvtgkbu3.jpg";
        userMapper.add(realname, password,address,phonenumber,avatar);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public void updatePwd(String newPwd){
        Map<String,Object> map = ThreadLocalUtil.get();
        int userid = (int) map.get("userid");
        userMapper.updatePwd(newPwd,userid);
    }

    @Override
    public void updatebalance(double money){
        Map<String,Object> map = ThreadLocalUtil.get();
        int userid = (int) map.get("userid");
        userMapper.updatebalance(money,userid);
    }

    @Override
    public List<User> findAllUsers(){
        return userMapper.findAllUsers();
    }

    @Override
    public void updateavatar(String avatarurl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        int userid=(int)map.get("userid");
        userMapper.updateavatar(avatarurl,userid);
    }

    @Override
    public PageBean<User> findByinfo(String realname, String phonenumber, String email, String address, int pageNum, int pageSize) {
        PageBean<User> pb = new PageBean<>();
        // 初始化分页插件，设置当前页码和每页显示的数量
        PageHelper.startPage(pageNum, pageSize);

        // 根据用户ID和类别ID查询公告列表
        List<User> as = userMapper.findByinfo(realname,phonenumber,email,address);
        // 将查询结果转换为Page对象，以获取总记录数等分页信息
        Page<User> p = (Page<User>) as;

        // 将分页信息设置到PageBean中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());

        // 返回包含公告分页信息的PageBean对象
        return pb;
    }

    @Override
    public void updateStatus(int userid, int status) {
        userMapper.updateStatus(userid,status);
    }

    @Override
    public void updateaccountbalance(int userid, Double accountbalance) {
        userMapper.updateaccountbalance(userid,accountbalance);
    }

    @Override
    public void Updatebalance(Double balanceafter, Integer userid) {
        userMapper.updatebalance(balanceafter,userid);
    }


}
